﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Nyamr.Implementation
{
    class Emitter : IEmitter
    {
        public int NextID { get; set; }
        public StreamWriter Index { get; set; }
        public string Target { get; set; }
        
        public void Emit(string key, byte[] value)
        {
            lock (this)
            {
                Index.WriteLine(key);
                Index.WriteLine(NextID.ToString() + ".data");
                Index.Flush();

                var outputName = Path.Combine(Target, NextID.ToString() + ".data");
                var writer = new FileStream(outputName, FileMode.CreateNew);
                writer.Write(value, 0, value.Length);
                writer.Flush();
                writer.Close();

                NextID++;
            }
        }
    }
}
